package gui;
/**
 * guiLoginScreen.java
 * 
 * @author Evan Nelson
 */
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import database.DB_connection;


/**
 * This constructs the login screen panel
 * 
 * @author Evan Nelson
 *
 */
public class Main_LoginPanel extends JPanel {

	private static final long serialVersionUID = 3877849260468200960L;
	private JLabel exit;
	private JLabel login;
	private JLabel userLbl;
	private JLabel passLbl;
	private JLabel incorrectPasswordLbl;
	private JPasswordField passwordField;
	private JTextField userName;
	
	/**
	 * Constructor
	 *
	 */
	public Main_LoginPanel() {
		initComponents();
	}

	
	/**
	 * Initialize the panel
	 */
	private void initComponents() {
		//initialize the panel
		userLbl = new JLabel();
		userName = new JTextField("Evan Nelson");
		passLbl = new JLabel();
		incorrectPasswordLbl = new JLabel();
		passwordField = new JPasswordField("tmpPassword");
		exit = new JLabel();
		login = new JLabel();
		
		setLayout(null);
		login.setHorizontalAlignment(SwingConstants.LEFT);
		login.setText("Log In");
		login.setFont(new java.awt.Font("Tahoma", 1, 20));
		login.setHorizontalTextPosition(SwingConstants.CENTER);
		login.setVerticalAlignment(SwingConstants.BOTTOM);
		add(login);
		login.setBounds(15, 100, 70,70);

		exit.setHorizontalAlignment(SwingConstants.RIGHT);
		exit.setText("Exit");
		exit.setFont(new java.awt.Font("Tahoma", 1, 20));
		
		exit.setVerticalAlignment(SwingConstants.BOTTOM);
		add(exit);
		exit.setBounds(700, 100, 70,70);
		
		userLbl.setFont(new java.awt.Font("Tahoma", 0, 14));
		userLbl.setText("User Name:");
		add(userLbl);
		userLbl.setBounds(240, 40, 180, 40);

		add(userName);
		userName.setBounds(320, 40, 240, 30);

		passLbl.setFont(new java.awt.Font("Tahoma", 0, 14));
		passLbl.setText("Password:");
		add(passLbl);
		passLbl.setBounds(240, 100, 90, 30);

		add(passwordField);
		passwordField.setBounds(320, 100, 240, 30);
		
		incorrectPasswordLbl.setFont(new java.awt.Font("Tahoma", 0, 14));
		incorrectPasswordLbl.setText("I'm sorry, your password is incorrect. Please try again.");
		incorrectPasswordLbl.setForeground(Color.WHITE);
		add(incorrectPasswordLbl);
		incorrectPasswordLbl.setBounds(180, 120, 440, 40);

		login.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				buttonPressed(evt);
			}
		});

		exit.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				buttonPressed(evt);
			}
		});
	
		setBackground(Color.WHITE);
	
	}

	/**
	 * Event handler for mouse events that occur on this JPanel
	 *  
	 * @param evt - the mouse event that occured
	 */
	private void buttonPressed(MouseEvent evt) {
		String text;
		text = ((JLabel) evt.getSource()).getText();
		//System.out.println(text);
		if (text == "Log In") {
			checkLogin();
		} else if (text == "Exit") {
			System.exit(0);
		}
	}


	private void checkLogin() {
		DB_connection conn = new DB_connection();
		ResultSet rs;
		
		String password = new String(passwordField.getPassword());
		String username = userName.getText();
		
		try {
			rs = conn.executeCurrentStatement("SELECT PID FROM Person WHERE name = '" + username + "'"
					+ "AND password = md5('" + password + "')");
			if(rs.next()) {
				//there was a matching record, password good
				mainWindow.successfulLogin(rs.getInt("PID"), username);
			} else {
				//password must have been wrong
				incorrectPasswordLbl.setForeground(Color.RED);
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "There has been an error trying to connect to the database. Please contact software developers.", "Error", JOptionPane.ERROR_MESSAGE);
		}
		
		
	}
	
}